
 /**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */
package com.sys.midware.redis.core.operation.impl;

import org.apache.log4j.Logger;

import com.sys.midware.redis.core.RedisFactory;
import com.sys.midware.redis.core.operation.AbstractOperations;
import com.sys.midware.redis.core.operation.KeyOperations;
import com.sys.midware.redis.exceptions.BRedisException;

import redis.clients.jedis.JedisCluster;

/**
 * <b>Description：</b> key 操作类<br/>
 * <b>ClassName：</b> HashOperationsImpl <br/>
 * <b>@author：</b> jackyshang <br/>
 * <b>@date：</b> 2016年8月22日 下午4:11:23 <br/>
 * <b>@version: </b>  <br/>
 */
public class ClusterKeyOperationsImpl extends AbstractOperations implements KeyOperations{

    private static Logger logger = Logger.getLogger(StringsOperationsImpl.class);
    private ClusterKeyOperationsImpl() {
    };

    private static ClusterKeyOperationsImpl instance;
    
    /**
     * 获取单例工厂
     * @return
     * @throws Exception
     */
    public static ClusterKeyOperationsImpl getInstance() {
        if (instance != null)
            return instance;

        synchronized (ClusterKeyOperationsImpl.class) {
            if (instance == null) {
                instance = new ClusterKeyOperationsImpl();
            }
            return instance;
        }
    }
    
    @Override
    public <K> boolean exists(K key) {
        JedisCluster jedis = getClient4Cluster();
        try {
            boolean f= jedis.exists(rawKey(key));
            if(!f){
                f = jedis.exists((String)key);
            }
            return f;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K> K echo(K key) {
        JedisCluster jedis = getClient4Cluster();
        try {
            byte[] t = jedis.echo(rawKey(key));
            return deserializeKey(t);
        } catch (Exception e) {
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

//    @Override
//    public  Set<String> matchKeys(String keyRegex) {
//        JedisCluster jedis = getClient4Cluster();
//        try {
//            
//            return jedis.(keyRegex);
//        } catch (Exception e) {
//            throw new BRedisException(e);
//        } finally {
//            RedisFactory.close(jedis);
//        }
//    }


   
    
}
